﻿using BLL;
using DAL;
using Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO.Packaging;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace MDMUI.SingleBatch
{
    public partial class Rework : Form
    {
        private SingleBatchEntryBLL singleBatchEntryBLL = new SingleBatchEntryBLL();

        public Rework()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (int.TryParse(textBox1.Text, out int batchId))
            {
                // 调用 BLL 层的方法获取批次数据
                Batch batchData = singleBatchEntryBLL.showBatchMeg(batchId);
                if (batchData == null)
                {
                    MessageBox.Show("未找到批次数据");
                    return;
                }

                // 获取产品数据
                Product productData = singleBatchEntryBLL.GetProductById(batchData.ProductId);
                if (productData == null)
                {
                    MessageBox.Show("未找到产品数据");
                    return;
                }

                // 获取总流程id
                int totalProcessId = batchData.TotalProcessId;
                TotalProcess totalProcessData = singleBatchEntryBLL.GetTotalProcessById(totalProcessId);
                int stationId = totalProcessData.TotalProcessId;
                Station stationData = singleBatchEntryBLL.GetStationById(stationId);
                int productFlowId = productData.FlowId;
                int productStationId = productData.StationId;
                ProcessFlow FlowData = singleBatchEntryBLL.GetProcessFlowById(productFlowId);
                Station StationData = singleBatchEntryBLL.GetStationById(productStationId);

                // 获取总流程下的所有工站
                List<Station> stationTotolData = singleBatchEntryBLL.GetStationByTotolId(totalProcessId);
                int totalStationCount = stationTotolData.Count;

                // 检查 flowId 和 stationId 是否为总流程长度加 1
                if (productData.FlowId == totalStationCount + 1 && productData.StationId == totalStationCount + 1)
                {
                    MessageBox.Show("该产品已完成所有流程");
                    return; // 不继续执行后续代码
                }

                // 将数据分配给窗体上的控件
                batchType.Text = batchData.BatchType;
                verboseType.Text = "Production";
                quantity.Text = batchData.Quantity.ToString();
                subQuantity.Text = batchData.SubQuantity.ToString();
                unit.Text = batchData.Unit.ToString();
                lockingState.Text = "未锁定";
                productNum.Text = batchData.ProductId.ToString();
                workOrderNum.Text = batchData.WorkOrderId.ToString();
                goodNum.Text = batchData.GoodQuantity.ToString();
                noGoodNum.Text = batchData.NGQuantity.ToString();
                wipStatus.Text = "未锁定";
                repairStatus.Text = "NotOnhold";
                processFlow.Text = FlowData.FlowName;
                versionNum.Text = FlowData.ProcessFlowVersion;
                describe.Text = FlowData.Description;
                scrapQty.Text = "0";
                scrapSubQty.Text = "0";
                processStatus.Text = "Wait";
                reworkStatus.Text = "unRework";
                station.Text = StationData.StationName;
                stationVesion.Text = StationData.StationVersion;
                StationDescription.Text = StationData.StationDescription;

            }
            else
            {
                MessageBox.Show("请输入有效的批次数");
            }
        }

        private void panel3_Paint(object sender, PaintEventArgs e)
        {

        }

        private void Rework_Load(object sender, EventArgs e)
        {

            //添加总流程下拉
            List<TotalProcess> totalProcesses = singleBatchEntryBLL.GetAllTotalProcesses();
            comboBox1.DataSource = totalProcesses;
            comboBox1.ValueMember = "TotalProcessId";
            comboBox1.DisplayMember = "TotalProcessName";
            
            List<ProcessFlow> processFlows = singleBatchEntryBLL.GetAllProcessFlows();
            comboBox2.DataSource = processFlows;
            comboBox2.ValueMember = "FlowId";
            comboBox2.DisplayMember = "FlowName";
            comboBox2.SelectedIndex = 0; // 默认选中第一项
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBox1.SelectedItem is TotalProcess selectedProcess)
            {
                textBox2.Text = selectedProcess.TotalProcessVersion;
                textBox3.Text = selectedProcess.TotalProcessDescription;
            }
        }

        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBox2.SelectedItem is ProcessFlow selectedFlow)
            {
                textBox28.Text = selectedFlow.ProcessFlowVersion;
                textBox4.Text = selectedFlow.Description;
            }
        }
    }
}
